package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.Exception.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;


    /**
     * 根据id查询套餐信息和对应的菜品信息
     * */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //在基本套餐信息Setmeal表中查询信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        //在对应的菜品表中查询信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
    * 新增套餐，同时需要保存套餐和菜品的关联关系
    * */
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);
        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 修改套餐及对应的菜品信息
     * */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //修改setmeal表信息
        super.updateById(setmealDto);
        //修改setmealDish表信息
        //删除对应的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //新增对应的菜品
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : list) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishService.save(setmealDish);
        }

    }
    /**
     * 根据id删除套餐
     * */
    @Transactional
    @Override
    public void deleteWithDish(String ids) {
        String[] arr = ids.split(",");
        //查一次表，但无法显示什么套餐无法删除
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(Setmeal::getId,arr);
        queryWrapper1.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper1);
        if(count > 0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //查多次表，但可显示名字
        /*for (String s : arr) {
            Setmeal setmeal = this.getById(s);
            int count = setmeal.getStatus();
            if(count == 1){
                throw new CustomException(setmeal.getName()+"正在售卖中，不能删除");
            }
        }*/
        for (String s : arr) {
            this.removeById(s);
            LambdaQueryWrapper<SetmealDish> queryWrapper =new LambdaQueryWrapper<>();
            queryWrapper.like(SetmealDish::getSetmealId,s);
            setmealDishService.remove(queryWrapper);
        }
    }
}
